<script type="text/javascript">

	 function start() {			
		 simulate();
	 }

	function createPg() {
		var pg = {
			weapon: {}
		}
		//pg.coo = rand(7,10);
		//pg.ref = rand(7,10);
		//pg.str = rand(7,10);
		//pg.con = rand(7,10);
		
		pg.skl = rand(7,10); 	
			pg.foc = rand(2,3);  	
			pg.pow = rand(2,10);
		  
		return pg;
	}

	function statPg(pg) {
		if (!pg.coo) pg.coo = statOnTarget(pg.ref);
		 if (!pg.ref) pg.ref = statOnTarget(pg.coo);
		 if (!pg.str) pg.str = statOnTarget(pg.con);
		if (!pg.con) pg.con = statOnTarget(pg.str);
		 
		//if (!pg.crit) 		pg.crit = pg.str;
		if (!pg.precision)	pg.precision = (pg.coo) + pg.skl + pg.foc ;
		if (!pg.defense)	pg.defense = (pg.ref) + pg.skl + pg.foc;
		if (!pg.hp)			pg.hp = pg.con*20;

		 return pg;
	 }

	function equipPg(pg, defType) {
		if (!pg.dmg)	pg.dmg = pg.str + Math.floor(pg.str/2);
		if (!pg.abs)	pg.abs = Math.floor((pg.con+pg.str)/2)-5;
		if (!pg.rate)	pg.rate = 1;

		if (!defType) {
			if (pg.str-pg.ref >= 2) 		defType = 'shieldStr';
			else if (pg.con-pg.ref >= 2)   	defType = 'shieldCon';
			else if (pg.coo-pg.ref >= 2)    defType = 'parry';
			else 							defType = 'dodge';
		}

		pg.defType = defType;

		switch (pg.defType) {
			case 'shieldStr':  pg.defense += 2; break;
			case 'shieldCon':  pg.abs += 2 ;    break;
			case 'parry':  	   pg.defense += 10 - pg.str; break;
		}
		
		pg.precision += 10 - pg.str;

		return pg;
	}

	
	function ATKmidProfile(pg) {
	 	var pg = createPg();
		pg.con = 10;
		pg.coo = 10;
		pg.str = 10;
		pg.ref = 10;
	 	pg = statPg(pg);
	 	pg = equipPg(pg);
	 	return pg;
	 }
	 function DEFmidProfile(pg) {
	 	var pg = createPg();
		pg.con = 10;
		pg.ref = 10;
		pg.str = 10;
		pg.coo = 10;
	  	pg = statPg(pg);
		pg = equipPg(pg, 'dodge');
		//pg.defense = 'dodge';
	  	return pg;
	 }

	function simulate () {
		var atkWin = 0, 	defWin = 0;
		var atkMaxDmg = 0, 	defMaxDmg = 0;
		var atkMidDmg = 0, 	defMidDmg = 0;
		var atkMidDiff = 0, defMidDiff = 0;
	var atkDmgPerc=0,	defDmgPerc=0 

	var statDifference = 0;
		var iterations = 100000;
		for (var i = 0; i < iterations; i++) {
		var pga = ATKmidProfile();
		var pgb = DEFmidProfile();

		pga.stu = rand(3,16, pga.rate);
		pgb.stu = rand(3,16, pgb.rate);
		var atk = pga.precision + pga.pow + pga.stu;
		var def = pgb.defense + pgb.pow + pgb.stu;
		//console.log(atk, def, pgb.defType);
		var difference = atk-def;
		var finalDmg;

	 	if (difference > 0) {
			finalDmg = difference + (pga.dmg-pgb.abs);
		  	//finalDmg = Math.max(finalDmg, 0);
			//if (finalDmg>60) finalDmg = finalDmg*2;
	  		atkWin++;
	  		atkDmgPerc += Math.floor(finalDmg / pgb.hp * 100);
	  		atkMaxDmg = atkMaxDmg < finalDmg ? finalDmg : atkMaxDmg; 
	  		atkMidDmg += finalDmg;
	  		atkMidDiff += difference;
	 	}
		if (difference < 0 && pgb.defType == 'dodge') {
			finalDmg = difference - (pgb.dmg-pga.abs);
			//console.log(finalDmg, difference);
		 	//finalDmg = Math.min(finalDmg, 0);
			//if (finalDmg>60) finalDmg = finalDmg*2;
	 		defWin++; 
	 		defDmgPerc += Math.floor(finalDmg / pga.hp * 100);
	 		defMaxDmg = defMaxDmg > finalDmg ? finalDmg : defMaxDmg;  
	 		defMidDmg -= finalDmg;
	 		defMidDiff -= difference;
	  	}
		statDifference += (pga.str+pga.ref+pga.coo+pga.con)-(pgb.str+pgb.ref+pgb.coo+pgb.con);
		//console.log(pga, pgb);
		//console.log(pga.str,pga.ref,pga.coo,pga.con,')-(',pgb.str,pgb.ref,pgb.coo,pgb.con);
	 	//console.log(statDifference,' diff:', difference,' dmg:', finalDmg,' dmg %',atkDmgPerc||defDmgPerc, ' defense: ', pgb.defense);  	
	};

		atkMidDmg = Math.floor(atkMidDmg/atkWin);
		defMidDmg = Math.floor(defMidDmg/defWin);
		atkMidDiff = Math.floor(atkMidDiff/atkWin);
		defMidDiff = Math.floor(defMidDiff/defWin);
		atkDmgPerc = Math.floor((atkDmgPerc/atkWin));
		defDmgPerc = Math.floor((defDmgPerc/defWin));
		atkWin = Math.floor((atkWin/iterations)*100);
		defWin = Math.floor((defWin/iterations)*100);
	statDifference = Math.floor(statDifference/iterations);
	
		console.log('atkWin:',atkWin+ '% | atkMidDmg:',atkMidDmg+' | atkDmgPerc:',atkDmgPerc+ '% | atkMidDiff:',atkMidDiff, ' | atkMaxDmg:', atkMaxDmg+'%');
		console.log('defWin:',defWin+ '% | defMidDmg:',defMidDmg+' | defDmgPerc:',defDmgPerc+ '% | defMidDiff:',defMidDiff , ' | defMaxDmg:',defMaxDmg+'%');
		console.log('statDifference', statDifference);
	}

	 function rand(min, max, repeat) {
	 	if (!min) min = 1;
		if (!repeat) repeat = 1;
	 	max = max ? max-min+1 : 10;
		var result = 0;
		for (var i=0; i < repeat; i++) {
			var temp = Math.floor((Math.random()*max)+min)
			result = temp > result ? temp : result;
		}
		return result;
	 }
	function statOnTarget(targetStat) {
		if (targetStat <= 7) 			return rand(8,10);
		else if (targetStat <= 8) 	return rand(7,9);
		 else 		 					return rand(6,8);
	}

 </script>